package com.qf.service.impl;

import com.qf.entity.Order;
import com.qf.entity.WmsUser;
import com.qf.mapper.OrderMapper;
import com.qf.service.OrderService;
import com.qf.until.AliyunOSSUtil;
import com.qf.until.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * --- 天道酬勤 ---
 *
 * @author Xiaoze
 * @date 2024/7/23
 * @desc
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<Order> findAll(HashMap<String, String> map) {
        return orderMapper.findAll(map);
    }


    @Override
    public void addOrder(Order order) {
        order.setCreateTime(new Date());
        orderMapper.addOrder(order);
    }

    @Override
    public boolean delOrderById(int id) {
        return orderMapper.delOrderById(id);
    }

    @Override
    public boolean delBatch(List<Integer> ids) {
        return orderMapper.delBatch(ids);
    }

    @Override
    public List<Order> detail(int id) {
        return orderMapper.detail(id);
    }

    @Override
    public List<WmsUser> findUserName() {
        return orderMapper.findUserName();
    }

    @Override
    public boolean updateAr(Order order) {
        int q1 = order.getQuantity();
        int aQuantity = order.getAQuantity();
        order.setQuantity(q1 - aQuantity);

        order.setUQuantity(q1 - aQuantity);
        if (q1 == aQuantity) {
            order.setArrivalStatus("1");
        } else if (aQuantity > 0 && aQuantity < q1) {
            order.setArrivalStatus("3");
        }
        if(order.getArrivalStatus().equals("3")) {
           Order orders =orderMapper.findName(order.getOrderName());
           order.setAQuantity(orders.getAQuantity()+order.getAQuantity());
           order.setUQuantity(q1-order.getAQuantity());
           if (order.getQuantity() == orders.getAQuantity()){
               order.setArrivalStatus("1");
           }else if(orders.getAQuantity() > order.getQuantity()){
               return false;
           }
        }

        // orderMapper.findList(order.getArrivalStatus());

        order.setDeliveryTime(new Date());
        return orderMapper.updateAr(order);
    }

    @Override
    public void saveBatch(ArrayList<Order> list) {
        orderMapper.saveBatch(list);
    }

    @Override
    public List<Order> findAllList() {
        return orderMapper.findAllList();
    }
}
